package com.sbp.messsage_consumer_demo.service.bs;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dap.api.AbstractSunriseService;
import com.dap.exception.BusinessException;
import com.dap.exception.PlatformException;
import com.dap.param.StringInput;
import com.sbp.message.annotation.MessageConsumerAnnotation;
import org.mengyun.tcctransaction.api.TransactionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@MessageConsumerAnnotation(topics = {"xxx1", "xxx2"}, exclusive = true)
public class TestConsumer1ServiceImpl extends AbstractSunriseService {
    static final Logger logger = LoggerFactory.getLogger(TestConsumer1ServiceImpl.class);

    @Transactional
    public Object execute(TransactionContext transactionContext, StringInput stringInput) {
        logger.debug("TestConsumer1ServiceImpl, start ---------------");

        JSONObject jsonObject = JSON.parseObject(stringInput.getBody());
        String flagStr = jsonObject.getString("demo1.1");
        if (null != flagStr) {
            switch (flagStr) {
                case "BusinessException":
                    throw new BusinessException();
                case "PlatformException":
                    throw new PlatformException();
                default:
                    break;
            }
        }

        logger.debug("TestConsumer1ServiceImpl, end, stringInput.getBody() = {}", stringInput.getBody());
        return null;
    }
}
